<template>
    <div class="bgimgseeker">
        <div class="login">
            <div>
            <h3>用户登录</h3>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm">
                    <el-form-item prop="username" el-icon="el-icon-user">
                        <el-input placeholder="用户名" v-model="ruleForm.username" autocomplete="off" style="width: 300px;"></el-input>
                    </el-form-item>
                    <el-form-item prop="userpass" >
                        <el-input placeholder="密码" type="password" v-model="ruleForm.userpass"
                                  autocomplete="off" style="width: 300px"></el-input>
                    </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
    </div>
</template>

<script>
    export default {
        name: 'login',
        data() {
            return {
                activeIndex: '1',
                ruleForm: {
                    username: '',
                    userpass: '',

                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    userpass: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ],
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        var params= new URLSearchParams();
                        params.append('username',this.ruleForm.username,)
                        params.append('userpass',this.ruleForm.userpass,)
                        this.axios.post('http://localhost:8001/user/login',params).then((res)=>{
                            // console.log(res.data.msg);
                            if(res.data.msg==='success'){
                                // alert('登录成功');
                                // this.$store.dispatch('setUser', true)
                                // localStorage.setItem('Flag', 'isLogin')
                                this.$message({
                                    showClose: true,
                                    message:'登录成功',
                                    type: 'success'
                                });
                                localStorage.setItem('flag', 1)
                                localStorage.setItem('userid', res.data.userid)
                                localStorage.removeItem('compid');
                                console.log(localStorage.getItem('useid'))
                                this.$router.push({ path:'/'})
                                this.$router.go(0)

                            }
                            else {
                                this.$message({
                                    showClose: true,
                                    message: res.data.msg,
                                    type: 'error'
                                });
                                // alert('账号或密码错误');
                            }
                        }).catch((err)=>{
                            console.log('error'+err);
                        })
                    } else {
                        // alert('err');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
        }

    }
</script>
<style>
    .login {
        display: flex;
        flex-direction: row;
        justify-content: center;
        background: #fff;
        border: 1px solid #ededed;
        margin-left: 55%;
        margin-top: 5%;
        width: 500px;
        box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.1);
        border-radius: 8px;
        position: relative;
    }
</style>
